/*
 * Configuracoes_Base.java
 *
 * Created on 22 de Maio de 2007, 00:43
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.jerm.Interface;
import org.jerm.Conectividade.SGBD;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;

/**
 *
 * @author Leonardo
 */
public class Configuracoes_Base extends JPanel
{
    protected Principal Principal;
    private javax.swing.JButton button_test;
    private javax.swing.JButton button_text;
    private javax.swing.JButton button_rest;
    private javax.swing.JComboBox combo_sgbd;
    private javax.swing.JTextField edit_database;
    private javax.swing.JTextField edit_host;
    private javax.swing.JTextField edit_porta;
    private javax.swing.JTextField edit_projeto;
    private javax.swing.JPasswordField edit_senha;
    private javax.swing.JTextField edit_usuario;
    private javax.swing.JLabel nSGBD;
    private javax.swing.JLabel nPort;
    private javax.swing.JLabel nUsuario;
    private javax.swing.JLabel nSenha;
    private javax.swing.JLabel nBaseDados;
    private javax.swing.JLabel nStatus;
    private javax.swing.JLabel nProjeto;
    private javax.swing.JLabel label_status;
    private javax.swing.JPanel panel_login;

    private GridBagConstraints c;

    private SGBD conn;
    
    private Color COLOR_atencao;
    private Color COLOR_erro;
    private Color COLOR_sucesso;
    
    
    /** Creates a new instance of Configuracoes_Base */
    public Configuracoes_Base (Principal Principal)
    {
        super();
        this.Principal=Principal;
        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Banco de Dados para Base do Projeto"));
        COLOR_atencao=new java.awt.Color(51, 51, 255);
        COLOR_erro=new java.awt.Color(255, 51, 51);
        COLOR_sucesso=new java.awt.Color(0, 120, 0);
        setMinimumSize(new Dimension(400,300));
        setPreferredSize (new Dimension(400,300));
        initComponentes();
    }
    
    
    private void initComponentes(){
        AbstractAction action;
        panel_login = new javax.swing.JPanel();
        nSGBD = new javax.swing.JLabel();
        combo_sgbd = new javax.swing.JComboBox();
        nPort = new javax.swing.JLabel();
        edit_host = new javax.swing.JTextField();
        nProjeto = new javax.swing.JLabel();
        edit_projeto = new javax.swing.JTextField();
        edit_porta = new javax.swing.JTextField();
        nUsuario = new javax.swing.JLabel();
        edit_usuario = new javax.swing.JTextField();
        nSenha = new javax.swing.JLabel();
        nBaseDados = new javax.swing.JLabel();
        edit_database = new javax.swing.JTextField();
        button_text = new javax.swing.JButton();
        button_test = new javax.swing.JButton();
        button_rest = new javax.swing.JButton();
        nStatus = new javax.swing.JLabel();
        label_status = new javax.swing.JLabel();
        edit_senha = new javax.swing.JPasswordField();

        panel_login.setFont(new java.awt.Font("Tahoma", 3, 11));
        nSGBD.setFont(new java.awt.Font("Tahoma", 3, 11));
        nSGBD.setText("SGBD disponiveis");

        combo_sgbd.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "mysql", "postgresql" }));
        combo_sgbd.setName("");

        nPort.setFont(new java.awt.Font("Tahoma", 3, 11));
        nPort.setText("Host / Porta:");

        edit_host.setName("");

        edit_porta.setName("");

        nUsuario.setFont(new java.awt.Font("Tahoma", 3, 11));
        nUsuario.setText("Usuario:");

        edit_usuario.setName("");

        nSenha.setFont(new java.awt.Font("Tahoma", 3, 11));
        nSenha.setText("Senha:");

        nBaseDados.setFont(new java.awt.Font("Tahoma", 3, 11));
        nBaseDados.setText("Base de Dados:");

        edit_database.setName("");
        
        nProjeto.setFont(new java.awt.Font("Tahoma", 3, 11));
        nProjeto.setText("Projeto:");

        edit_projeto.setName("");

        action = new AbstractAction("Salvar"){
                public void actionPerformed(ActionEvent e) {
                    salvar();
                }
        };
        button_text.setAction(action);

        action = new AbstractAction("Testar"){
                public void actionPerformed(ActionEvent e) {
                    testar();
                }
        };
        button_test.setAction(action);
        
        action = new AbstractAction("Restaurar"){
                public void actionPerformed(ActionEvent e) {
                    restaurar();
                }
        };
        button_rest.setAction(action);
        
        
        nStatus.setFont(new java.awt.Font("Tahoma", 3, 11));
        nStatus.setText("Status:");

        label_status.setFont(new java.awt.Font("Tahoma", 3, 11));
        
        
        this.setLayout(new GridBagLayout());
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        
        c.insets=new Insets(5,5,1,1);
        c.gridx = 0; //posicao x na linha
        c.gridy = 0; //posicao y na linha
        add(nSGBD,c);
        
        c.gridx=1;
        c.gridwidth=2;
        add(combo_sgbd,c);
        c.gridwidth=1;
        
        c.gridy++;///////////////////////////////////////
        
        c.gridx = 0; //posicao y na linha
        add(nPort,c);
        
        c.gridx=1;
        add(edit_host,c);
        
        c.gridx=2;
        add(edit_porta,c);
        
        c.gridy++;///////////////////////////////////
        
        c.gridx = 0; //posicao y na linha
        add(nBaseDados,c);
        
        c.gridx=1;
        c.gridwidth=2;
        add(edit_database,c);
        c.gridwidth=1;
        
        c.gridy++;///////////////////////////////////
        
        c.gridx = 0; //posicao y na linha
        add(nProjeto,c);
        
        c.gridx=1;
        c.gridwidth=2;
        add(edit_projeto,c);
        c.gridwidth=1;
        
        c.gridy++;////////////////////////////////////////
        
        c.gridx = 0; //posicao y na linha
        add(nUsuario,c);
        
        c.gridx=1;
        c.gridwidth=2;
        add(edit_usuario,c);
        c.gridwidth=1;
        
        c.gridy++;////////////////////////////////////////
        
        c.gridx = 0; //posicao y na linha
        add(nSenha,c);
        
        c.gridx=1;
        c.gridwidth=2;
        add(edit_senha,c);
        c.gridwidth=1;
        
        c.gridy++;////////////////////////////////////////
        
        c.gridx = 0; //posicao y na linha
        add(button_text,c);
        
        c.gridx=1;
        add(button_rest,c);
        
        c.gridx=2;
        add(button_test,c);
        
        c.gridy++;////////////////////////////////////////
        
        c.gridx = 0; //posicao y na linha
        add(nStatus,c);
        
        c.gridx=1;
        c.gridwidth=2;
        add(label_status,c);
        c.gridwidth=1;
        
        
    }
    
    private void salvar(){
        Principal.SGBD_BASE=new SGBD(combo_sgbd.getSelectedItem().toString(), edit_host.getText(), edit_usuario.getText(),String.valueOf(edit_senha.getPassword()),edit_porta.getText(), edit_database.getText());
        Principal.Projeto=edit_projeto.getText();
        Principal.SGBD_BASE.Conecta();
        restaurar();
    }
    
    public void restaurar(){
        combo_sgbd.setSelectedItem(Principal.SGBD_BASE.getSGBD());
        edit_host.setText(Principal.SGBD_BASE.getServidor());
        edit_usuario.setText(Principal.SGBD_BASE.getUsuario());
        edit_senha.setText(Principal.SGBD_BASE.getSenha());
        edit_porta.setText(Principal.SGBD_BASE.getPorta());
        edit_database.setText(Principal.SGBD_BASE.getBD());
        edit_projeto.setText(Principal.Projeto);
        
        if (Principal.SGBD_BASE.getStatus())
        {
            label_status.setText("Conectado.");
            label_status.setForeground(COLOR_sucesso);
        }
        else
        {
            label_status.setText("Desconectado.");
            label_status.setForeground(COLOR_erro);
        }
    }
    
    private void testar(){
        try
        {
            conn = new SGBD(combo_sgbd.getSelectedItem().toString(), edit_host.getText(), edit_usuario.getText(),String.valueOf(edit_senha.getPassword()),edit_porta.getText(), edit_database.getText());
            //conn.Desconecta();
            String Resultado=conn.ConectaTeste();

            label_status.setText(Resultado);
            if (conn.getRealStatus())
            {
                label_status.setForeground(COLOR_sucesso);
            }
            else
            {
                label_status.setForeground(COLOR_atencao);
            }
        }
        catch(Exception e)
        {
            label_status.setText("Verifique as informacoes e tente novamente...");
            label_status.setForeground(COLOR_atencao);
        }
    }
}
